class PracticeRecord {
  final int? id;
  final int questionId;
  final String userAnswer;
  final String correctAnswer;
  final bool isCorrect;
  final int timeSpent; // 答题用时（秒）
  final int createdAt;

  PracticeRecord({
    this.id,
    required this.questionId,
    required this.userAnswer,
    required this.correctAnswer,
    required this.isCorrect,
    required this.timeSpent,
    required this.createdAt,
  });

  // 从Map创建PracticeRecord对象
  factory PracticeRecord.fromMap(Map<String, dynamic> map) {
    return PracticeRecord(
      id: map['id']?.toInt(),
      questionId: map['question_id']?.toInt() ?? 0,
      userAnswer: map['user_answer'] ?? '',
      correctAnswer: map['correct_answer'] ?? '',
      isCorrect: map['is_correct'] == 1 || map['is_correct'] == true,
      timeSpent: map['time_spent']?.toInt() ?? 0,
      createdAt: map['created_at']?.toInt() ?? DateTime.now().millisecondsSinceEpoch,
    );
  }

  // 转换为Map
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'question_id': questionId,
      'user_answer': userAnswer,
      'correct_answer': correctAnswer,
      'is_correct': isCorrect ? 1 : 0,
      'time_spent': timeSpent,
      'created_at': createdAt,
    };
  }

  // 复制并修改
  PracticeRecord copyWith({
    int? id,
    int? questionId,
    String? userAnswer,
    String? correctAnswer,
    bool? isCorrect,
    int? timeSpent,
    int? createdAt,
  }) {
    return PracticeRecord(
      id: id ?? this.id,
      questionId: questionId ?? this.questionId,
      userAnswer: userAnswer ?? this.userAnswer,
      correctAnswer: correctAnswer ?? this.correctAnswer,
      isCorrect: isCorrect ?? this.isCorrect,
      timeSpent: timeSpent ?? this.timeSpent,
      createdAt: createdAt ?? this.createdAt,
    );
  }

  @override
  String toString() {
    return 'PracticeRecord{id: $id, questionId: $questionId, isCorrect: $isCorrect}';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is PracticeRecord && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}